package dao;

import cn.hutool.crypto.digest.MD5;
import util.DbUtilHikari;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import cn.hutool.crypto.SecureUtil;

public class UserDao {


    /**
     *
     * @param uname
     * @param upsw
     * @return 0 失败 1 成功
     */
    public int insertUser(String uname,String upsw){

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int flag = 0;

        try {
            connection = DbUtilHikari.getConnection();
            String sql = "insert into users(username,userpsw) values(?,?)";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,uname);
            preparedStatement.setString(2, MD5.create().digestHex(upsw));
            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (preparedStatement != null && connection != null){
                DbUtilHikari.close(preparedStatement,connection);
            }
        }

        return flag;
    }
}